package com.taoyibao.hongcanting.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.utils.CodeUtils;

/**
 * @author TangAnna
 * @description: 自定义TextView忧伤角有文字的textView
 * @date :${DATA} 15:04
 */
public class CountText extends RelativeLayout {
    private TextView mTvText;
    private TextView mTvCount;
    private View line;//底线
    private int defaultTextColor = CodeUtils.getIdColor(R.color.color999);//未选中的文字颜色
    private int selectTextColor = CodeUtils.getIdColor(R.color.colorf46);//选中的文字颜色
    private boolean isSelect = false;//是否是默认选中的状态

    public CountText(Context context) {
        super(context);
    }

    public CountText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public CountText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        inflate(context, R.layout.area_count_text, this);
        mTvText = findViewById(R.id.tv_text);
        mTvCount = findViewById(R.id.tv_count);
        line = findViewById(R.id.view_line);
        //获取自定义属性
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CountText);
        String text = typedArray.getString(R.styleable.CountText_text);
        String count = typedArray.getString(R.styleable.CountText_count);
        defaultTextColor = typedArray.getColor(R.styleable.CountText_defaultTextColor, defaultTextColor);
        selectTextColor = typedArray.getColor(R.styleable.CountText_selectTextColor, selectTextColor);
        isSelect = typedArray.getBoolean(R.styleable.CountText_isSelect, isSelect);
        setSelected(isSelect);
        setViewText(text);
        setCount(count, true);
        typedArray.recycle();
    }

    /**
     * 设置文字
     *
     * @param text
     */
    public void setViewText(String text) {
        if (TextUtils.isEmpty(text)) {
            return;
        }
        mTvText.setText(text);
    }

    /**
     * 设置数量
     *
     * @param count 数量
     * @param b     数量为零时是否隐藏  true隐藏  false 显示
     */
    public void setCount(String count, boolean b) {
        if (TextUtils.isEmpty(count)) {
            mTvCount.setVisibility(GONE);
            return;
        }
        mTvCount.setText(count);
        mTvCount.setVisibility(TextUtils.equals(count, "0") && b ? GONE : VISIBLE);
    }

    /**
     * 设置是否是选中状态
     *
     * @param isSelect true 选中状态  false 未选中状态
     */
    public void setSelected(boolean isSelect) {
        if (isSelect) {//选中
            mTvText.setTextColor(selectTextColor);
        } else {
            mTvText.setTextColor(defaultTextColor);
        }
        //选中状态设置显示底线，未选中状态隐藏底线
        line.setVisibility(isSelect ? VISIBLE : GONE);
    }


}
